package com.jingdianjichi.subject.infra.basic.service.impl;

import com.jingdianjichi.subject.infra.basic.entity.SubjectInfoEsSpringboot;
import com.jingdianjichi.subject.infra.basic.esRepo.SubjectEsRepository;
import com.jingdianjichi.subject.infra.basic.service.SubjectEsServiceSpringBoot;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: //todo
 * @Author: 张广焱
 * @Date: 2024-02-25 9:49
 */
@Service
@Slf4j
public class SubjectEsServiceSpringBootImpl implements SubjectEsServiceSpringBoot {
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate; // 对索引有操作的

    @Resource
    private SubjectEsRepository subjectEsRepository;

    @Override
    public void createIndex() {

        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(SubjectInfoEsSpringboot.class);
        //创建索引
        indexOperations.create();
        //创建映射
        Document mapping = indexOperations.createMapping(SubjectInfoEsSpringboot.class);
        indexOperations.putMapping(mapping);
    }

    @Override
    public void addDocs() {
        List<SubjectInfoEsSpringboot> list = new ArrayList<>();
        list.add(new SubjectInfoEsSpringboot(1L, "reids是什么", "redis是缓存", "张", new Date()));
        list.add(new SubjectInfoEsSpringboot(2L, "reids是什么", "redis是缓存", "张", new Date()));
        subjectEsRepository.saveAll(list);
    }

    /**
     * @Description: 查询文档
     * @Param:
     * @Author: 张广焱
     * @Date: 2024-02-25  13:04:20
     */
    @Override
    public void find() {
        //查询文档
        Iterable<SubjectInfoEsSpringboot> all = subjectEsRepository.findAll();
        for (SubjectInfoEsSpringboot subjectInfoEsSpringboot : all) {
            log.info("subjectInfoEsSpringboot:{}",subjectInfoEsSpringboot);
        }
    }

    /**
     * @Description: 搜索关键词
     * @Param:
     * @Author: 张广焱
     * @Date: 2024-02-25  13:04:06
     */
    @Override
    public void search() {
        //查询条件
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("subjectName", "redis"))
                .build();
        SearchHits<SubjectInfoEsSpringboot> search = elasticsearchRestTemplate
                .search(nativeSearchQuery, SubjectInfoEsSpringboot.class);
        List<SearchHit<SubjectInfoEsSpringboot>> searchHits = search.getSearchHits();
        log.info("searchHits:{}", searchHits);
    }
}
